package com.example.industry.controller;

import com.example.industry.entity.Project;
import com.example.industry.entity.User;
import com.example.industry.enums.Roles;
import com.example.industry.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController
{
    @Resource
    private UserService userService;

    /**
     * 通过Id查找用户
     * @param userId 用户Id
     * @return 用户
     */
    @GetMapping(value = "/{userId}")
    public User findUserById(@PathVariable("userId") Integer userId)
    {
        return userService.findUserById(userId);
    }

    /**
     * 注册
     * @param username 用户名
     * @param email 邮箱
     * @param password 密码
     * @return 是否成功
     */
    @PostMapping(value = "/register")
    public boolean register(@RequestParam(value = "username") String username,
                            @RequestParam(value = "email") String email,
                            @RequestParam(value = "password") String password)
    {
        return userService.register(username, email, password);
    }

    /**
     * 登陆
     * @param email 邮箱
     * @param password 密码
     * @param session 会话
     * @return 是否成功
     */
    @PostMapping(value = "/login")
    public boolean login(@RequestParam(value = "email") String email,
                         @RequestParam(value = "password") String password, HttpSession session)
    {
        return userService.login(email, password, session);
    }

    /**
     * 注销
     * @param session 会话
     */
    @PostMapping(value = "/logout")
    public void logout(HttpSession session)
    {
        userService.logout(session);
    }

    /**
     * 获取用户参与的项目, 指定角色
     * @param userId 用户Id
     * @param role 角色
     * @return 项目集合
     */
    @GetMapping(value = "/projects")
    public List<Project> findProjectsByUserIdAndRole(@RequestParam(value = "userId") Integer userId,
                                                     @RequestParam(value = "role") Roles role)
    {
        return userService.findProjectsByUserIdAndRole(userId, role);
    }
}
